package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.model.ShopCategory;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.ShopCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺分类Controller
 */
@RestController
@RequestMapping("/shopCategory")
@Api(tags = "商品管理模块-商品分类接口")
public class ShopCategoryController {
    
    @Autowired
    private ShopCategoryService shopCategoryService;
    
    @GetMapping("/page")
    @ApiOperation(value = "分页查询店铺分类", notes = "根据条件分页查询店铺分类列表")
    public ResponseEntity<IPage<ShopCategory>> page(
            @ApiParam(value = "页码", required = true) @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页数量", required = true) @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam(value = "商品类型0虚拟商品1实物商品") @RequestParam(required = false) Integer prodType,

            @ApiParam(value = "店铺ID") @RequestParam(required = false) Long shopId,
            @ApiParam(value = "分类名称") @RequestParam(required = false) String categoryName,
            @ApiParam(value = "状态：0-禁用，1-启用") @RequestParam(required = false) Integer status) {
        
        Page<ShopCategory> page = new Page<>(pageNum, pageSize);
        ShopCategory shopCategory = new ShopCategory();
        shopCategory.setShopId(SecurityUtils.getShopUser().getShopId());
        shopCategory.setCategoryName(categoryName);
        shopCategory.setStatus(status);
        
        IPage<ShopCategory> categoryPage = shopCategoryService.listCategoryPage(page, shopCategory);
        return ResponseEntity.ok(categoryPage);
    }
    
    @GetMapping("/list")
    @ApiOperation(value = "获取店铺分类列表", notes = "根据店铺ID获取分类列表")
    public ResponseEntity<List<ShopCategory>> list(
            @ApiParam(value = "店铺ID", required = true) @RequestParam Long shopId) {
        
        List<ShopCategory> categoryList = shopCategoryService.listByShopId(shopId);
        return ResponseEntity.ok(categoryList);
    }
    
    @GetMapping("/info")
    @ApiOperation(value = "获取分类详情", notes = "根据分类ID获取分类详情")
    public ResponseEntity<ShopCategory> info(
            @ApiParam(value = "分类ID", required = true) @RequestParam Long id) {
        
        ShopCategory shopCategory = shopCategoryService.getById(id);
        if (shopCategory == null) {
            throw new YamiShopBindException("shop.category.not.exist");
        }
        
        return ResponseEntity.ok(shopCategory);
    }
    
    @PostMapping("/save")
    @ApiOperation(value = "添加店铺分类", notes = "添加新的店铺分类")
    public ResponseEntity<Void> save(@RequestBody ShopCategory shopCategory) {
        if (shopCategory.getCategoryName() == null || shopCategory.getCategoryName().trim().isEmpty()) {
            throw new YamiShopBindException("shop.category.name.empty");
        }
        
        // if (shopCategory.getShopId() == null) {
        //     throw new YamiShopBindException("shop.id.not.null");
        // }
        shopCategory.setShopId(SecurityUtils.getShopUser().getShopId());
        boolean result = shopCategoryService.saveCategory(shopCategory);
        if (!result) {
            throw new YamiShopBindException("shop.category.save.error");
        }
        
        return ResponseEntity.ok().build();
    }
    
    @PostMapping("/update")
    @ApiOperation(value = "更新店铺分类", notes = "更新店铺分类信息")
    public ResponseEntity<Void> update(@RequestBody ShopCategory shopCategory) {
        if (shopCategory.getId() == null) {
            throw new YamiShopBindException("shop.category.id.not.null");
        }
        
        if (shopCategory.getCategoryName() == null || shopCategory.getCategoryName().trim().isEmpty()) {
            throw new YamiShopBindException("shop.category.name.empty");
        }
        
        boolean result = shopCategoryService.updateCategory(shopCategory);
        if (!result) {
            throw new YamiShopBindException("shop.category.update.error");
        }
        
        return ResponseEntity.ok().build();
    }
    
    @PostMapping("/delete")
    @ApiOperation(value = "删除店铺分类", notes = "根据分类ID删除店铺分类")
    public ResponseEntity<Void> delete(
            @ApiParam(value = "分类ID", required = true) @RequestParam Long id) {
        
        boolean result = shopCategoryService.deleteCategory(id);
        if (!result) {
            throw new YamiShopBindException("shop.category.delete.error");
        }
        
        return ResponseEntity.ok().build();
    }
    
    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除店铺分类", notes = "批量删除多个店铺分类")
    public ResponseEntity<Void> deleteBatch(
            @ApiParam(value = "分类ID列表", required = true) @RequestBody List<Long> ids) {
        
        boolean result = shopCategoryService.batchDeleteCategory(ids);
        if (!result) {
            throw new YamiShopBindException("shop.category.delete.error");
        }
        
        return ResponseEntity.ok().build();
    }
    
    @PostMapping("/updateStatus")
    @ApiOperation(value = "更新分类状态", notes = "更新店铺分类的启用/禁用状态")
    public ResponseEntity<Void> updateStatus(
            @ApiParam(value = "分类ID", required = true) @RequestParam Long id,
            @ApiParam(value = "状态：0-禁用，1-启用", required = true) @RequestParam Integer status) {
        
        if (status != 0 && status != 1) {
            throw new YamiShopBindException("shop.category.status.invalid");
        }
        
        boolean result = shopCategoryService.updateStatus(id, status);
        if (!result) {
            throw new YamiShopBindException("shop.category.status.update.error");
        }
        
        return ResponseEntity.ok().build();
    }
} 